package com.wechat.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.blade.kit.json.JSONObject;
import com.lkx.BeanHelper;
import com.lkx.domain.WxMsgDO;
import com.lkx.service.WxMsgService;
import com.wechat.util.Enums.MsgType;

public class TxtMessageHandler extends AbstractMessageHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(TxtMessageHandler.class);

	// private AccountConfigDao accountConfigDao = new AccountConfigDao();

	public TxtMessageHandler(WechatMeta meta) {
		super(meta);
		this.meta = meta;
	}

	@Override
	public void process(JSONObject msg) {
		String content = msg.getString("Content");
		LOGGER.info("content:{}", content);
		if (!preHandle(msg)) {
			return;
		}

		String fromUserName = msg.getString("FromUserName");
		String fromNickName = getUserRemarkName(fromUserName);
		String selftName = getSelfNickName();
		String[] contentArray = content.split(":");
		if (contentArray.length == 0 || contentArray.length == 1) {
		} else {
			content = contentArray[1].replace("<br/>", "\n");
		}
		PipeLineManager.instance().process(meta, fromNickName + "-" + selftName + ":" + content, MsgType.TXT);
		// String autoReplay = accountConfigDao.selectOne(meta.getMobile(),
		// AccountConfig.NAME_AUTO_REPLAY);
		// if (!AccountConfig.ON.equals(autoReplay)) {
		// LOGGER.warn("auto reply setting was off,you can switch on it");
		// return;
		// }
		String ans = localMsg(fromUserName, selftName, content);

		System.out.println("ans:======>" + ans + "[]" + msg.getString("FromUserName"));

		try {
			WxMsgService wxMsgService = BeanHelper.getBean("wxMsgService", WxMsgService.class);
			JSONObject contact = WxLocalCache.instance().getContactByUserName(fromUserName);

			WxMsgDO wxMsgDo = new WxMsgDO();
			String nickName = contact == null ? fromUserName :contact.getString("NickName", "[无名氏]");
			wxMsgDo.setFromusername(nickName);
			wxMsgDo.setTousername(selftName);
			wxMsgDo.setMsgContent(content);
			wxMsgService.save(wxMsgDo);

		} catch (Exception e) {
			System.out.println("保存消息异常:" + e.getMessage());
		}

		// 如果是群组需要@
		String pre = msg.getString("FromUserName").substring(0, 2);
		if (("@@").equals(pre)) {
			if (content.contains("@" + selftName)) {
				webwxsendmsg(ans + "【小承】", msg.getString("FromUserName"));
				PipeLineManager.instance().process(meta, selftName + "-" + fromNickName + "：" + ans, MsgType.TXT);
			}
		} else {
			webwxsendmsg(ans + "【小承】", msg.getString("FromUserName"));
			PipeLineManager.instance().process(meta, selftName + "-" + fromNickName + "：" + ans, MsgType.TXT);
		}

	}

	private String localMsg(String fromUserName, String selftName, String msg) {

		/*if (msg.contains("你是谁")) {
			msg = "我是小素，我五个月了,我爸爸是素剑步青尘,他是软件工程师。";
		} else if (msg.contains("你爸爸是谁")) {
			msg = "我是小素，我五个月了,我爸爸是素剑步青尘,他是软件工程师。";
		} else if (msg.contains("介绍")) {
			msg = "我是小素，我五个月了,我爸爸是素剑步青尘,他是软件工程师。";
		} else if (msg.contains("叫什么")) {
			msg = "我是小素，我五个月了,我爸爸是素剑步青尘,他是软件工程师。";
		} else if(msg.contains("你爸")){
			msg = "我是小素，我五个月了,我爸爸是素剑步青尘,他是软件工程师。";
		} else if(msg.contains("你多大")){
			msg = "我五个月了,你叫我哥哥";
		}else if(msg.contains("叫爸") || msg.contains("叫爹")){
			msg = "什么？不要上来就叫我爸爸,毕竟我才五个月呢";
		}else if(msg.contains("是你爸") || msg.contains("是你爹")){
			msg = "什么？不要上来就叫我爸爸,毕竟我才五个月呢";
		}else {
			msg = msg.replace(("@" + selftName), "");
			msg = reply(fromUserName, msg);
		}
		
		if(msg.contains("eno")){
			msg = msg.replace("eno", "小素");
		}*/
		
		msg = reply(fromUserName, msg);
		return msg;
	}

}
